<template>
	<view class="page-container">
		<!-- 顶部导航栏 -->
		<view class="header">
			<x-status-bar></x-status-bar>
			<view class="flex-center-between header-navbar">
				<x-back-btn></x-back-btn>
				<view class="search-container flex-center linecamp-1" style="margin: 0 16rpx;width: 100%;"
					@click="jumpUrl('/pages/user/order/search/index/index')">
					<image src="@/static/image/common/search.png" style="width: 32rpx; height: 32rpx"
						class="clear-flex u-m-r-14"></image>
					<view class="color-grew linecamp-1">{{ lang_order.searchTips }}</view>
				</view>
			</view>

			<!-- tabs -->
			<v-tabs v-model="tabIndex" :tabs="tabList" activeColor="#333333" lineBottom="12"></v-tabs>
			<!-- 分割线 -->
			<u-gap height="1" bgColor="rgba(0, 0, 0, 0.07)"></u-gap>
		</view>
		<!-- 列表 -->
		<swiper style="height: calc(100vh - 200rpx - var(--status-bar-height));" :current="tabIndex"
			@change="tabIndex = $event.detail.current">
			<swiper-item v-for="(item, index) in tabs" :key="index">
				<order-list ref="mescrollItem" :i="index" :index="tabIndex" :status-obj="item" :order-number="orderNumber" @change="changeAction"></order-list>
			</swiper-item>
		</swiper>
		
		<!-- 按钮操作 -->
		<order-deal v-model="visible" :type="typeKey" :order-number="orderNumber" @change="orderNumber = ''"></order-deal>
		<x-pay-select v-model="selectVisible" :info="info" @change="changeSelect"></x-pay-select>
		<x-share v-model="shareVisible"></x-share>
	</view>
</template>

<script>
	import OrderList from './components/order-list.vue'
	import { payAction } from '@/utils/pay.js'
	export default {
		components: {
			OrderList
		},
		data() {
			return {
				tabs: [],
				tabIndex: 0,
				tabValue: 'all',
				
				visible: false,
				typeKey: '',
				orderNumber: '',
				
				info: {},
				selectVisible: false,
				
				shareVisible: false,
				
				timer: null
			}
		},
		computed: {
			tabList() {
				return this.tabs.map(item => item.title)
			}
		},
		// 获取类型
		onLoad({
			type
		}) {
			// 初始化顶部导航栏
			let tabs = [
				{ key: 'all', label: '全部', value: '' },
				{ key: 'pendingPayment', label: '待付款', value: 0 },
				{ key: 'waitCollage', label: '待拼团', value: 1 },
				{ key: 'delivery', label: '待发货', value: 2 },
				{ key: 'receivedGoods', label: '待收货', value: 3 },
				{ key: 'beEvaluate', label: '待评价', value: 4 },
				{ key: 'refundSaled', label: '退款/售后', value: 'refund' },
			].map((item, index) => {
				let result = {
					...item,
					title: !index ? this.lang_common[item.key] : this.lang_order[item.key]
				}
				return result
			})
			this.tabs = tabs
			if (type) {
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.tabValue = type
				}, 300)
				this.tabIndex = this.tabs.findIndex(item => item.key === type)
			}
		},
		onShow() {
			this.orderNumber = '1'
			clearTimeout(this.timer)
			this.timer = setTimeout(() => {
				this.orderNumber = ''
			})
		},
		methods: {
			changeAction({ e: { key, orderNumber }, info }) {
				this.info = info
				this.typeKey = key
				this.orderNumber = orderNumber
				if (key === 'toPay') {
					this.selectVisible = true
				} else if(key === 'inviteFriend') {
					this.shareVisible = true
				} else {
					this.visible = true
				}
			},
			// 点击确定后
			changeSelect(type) {
				console.log(type)
				payAction(type, this.orderNumber).finally(() => {
					this.selectVisible = false
					this.orderNumber = ''
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.page-container /deep/ .u-drawer-bottom,
	.page-container /deep/ .u-mode-center-box {
		background: transparent
	}
	.search-container {
		width: 460rpx;
		padding: 16rpx 30rpx;
		border-radius: 100rpx;
		background-color: #F6F6F6;
	}

	.scroll-container {
		position: relative;
		width: 100vw;
	}

	.tab {
		&-container {
			position: relative;
			padding: 16rpx 0 30rpx;
		}

		&-item {
			position: relative;
			top: 0;
			left: 0;
			padding: 0 48rpx;
			width: max-content;

			&-line {
				position: absolute;
				bottom: -16rpx;
				left: 50%;
				transform: translateX(-50%);
				width: 42rpx;
				height: 8rpx;
				background: #333333;
				border-radius: 10rpx;
			}
		}
	}
</style>
